import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect:"/login"
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    redirect:"/home/goods",
    component: () => import(/* webpackChunkName: "home" */ '../views/home.vue'),
    children:[
      {
        path:"goods",
        name:"goods",
        meta:{
          isShow:true,
          title:"商品列表"
        },
       component: () => import(/* webpackChunkName: "goods" */ '../views/goods-view.vue'),
      },
      {
        path:"user",
        name:"user",
        meta:{
          isShow:true,
          title:"用户列表"
        },
       component: () => import(/* webpackChunkName: "user" */ '../views/user-view.vue'),
      },
      {
        path:"role",
        name:"role",
        meta:{
          isShow:true,
          title:"角色列表"
        },
       component: () => import(/* webpackChunkName: "user" */ '../views/role-view.vue'),
      },
      {
        path:"auth",
        name:"auth",
        meta:{
          isShow:false,
          title:"权限列表"
        },
       component: () => import(/* webpackChunkName: "user" */ '../views/auth-view.vue'),
      }

    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫，用作是否登陆查询
router.beforeEach((to,from,next)=>{
  const token: string | null = localStorage.getItem("token");
  if(!token&&to.path!=="/login"){
    next({name:'login'})
  }else{
    next()
  }

})

export default router
